package com.taxi.driver.module.main.mine.setting;

import android.app.Activity;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.model.LatLng;
import com.qianxx.utils.RxUtil;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.config.BizStatus;
import com.taxi.driver.config.PositionType;
import com.taxi.driver.data.amap.AMapManager;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.socket.message.UploadLocationMessage;
import com.taxi.driver.socket.message.base.MessageType;

import javax.inject.Inject;

public class SettingPresenter extends BasePresenter implements SettingContract.Presenter {

    private final SettingContract.View mView;
    private final UserRepository mUserRepository;
    private final AMapManager mAMapManager;

    @Inject
    public SettingPresenter(SettingContract.View view, UserRepository userRepository,
                            AMapManager aMapManager) {
        mView = view;
        mUserRepository = userRepository;
        mAMapManager = aMapManager;
    }

    @Override
    public void reqLogout() {
        LatLng latLng = new LatLng(mAMapManager.getLastLocation().getLatitude(), mAMapManager.getLastLocation().getLongitude());
        UploadLocationMessage uploadLocationMessage = new UploadLocationMessage();
        uploadLocationMessage.setAppid(((Activity) mView).getString(R.string.yueyue_appkey));
        uploadLocationMessage.setBizStatus(BizStatus.STOP_BUSINESS);
        uploadLocationMessage.setAreaCode(mAMapManager.getLastLocation().getAdCode());
        uploadLocationMessage.setDriverType(mUserRepository.getDriverType());
        uploadLocationMessage.setLat(latLng.latitude);
        uploadLocationMessage.setLng(latLng.longitude);
        uploadLocationMessage.setPositionType(PositionType.SJXB);
        uploadLocationMessage.setDistance(0d);
        uploadLocationMessage.setCarLevelType(mUserRepository.getCarLevelType());
        uploadLocationMessage.setClientUuid(mUserRepository.getLocalDriverUuid());
        uploadLocationMessage.setDriverUuid(mUserRepository.getLocalDriverUuid());
        uploadLocationMessage.setLocationUuid(System.currentTimeMillis() + "");
        uploadLocationMessage.setType(MessageType.UPLOAD_LOCATION);
        String uploadText = JSON.toJSONString(uploadLocationMessage);

        mUserRepository.reqLogout(latLng.longitude, latLng.latitude, uploadText)
                .compose(RxUtil.applySchedulers())
                .doOnSubscribe(() -> mView.showLoadingView(true))
                .doAfterTerminate(mView::hideLoadingView)
                .subscribe(s -> mView.logoutSuccess(),
                        ex -> showNetworkError(ex, R.string.network_error, mView, mUserRepository));
    }

    @Override
    public void setScreenStatue(boolean isBright) {
        mUserRepository.setScreenStatue(isBright);
    }

    @Override
    public boolean getScreenStatue() {
        return mUserRepository.getScreenStatue();
    }

}
